﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using KenLib.BusinessObject;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.ComponentModel;


public partial class JOINT_frmJ_TraineeAppAssignConfirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeInterviewList");
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("面接情報確認・承認");
        RFV_ddl_組合選択.ErrorMessage = GetError("ER00007");
        RFV_組合員企業選択.ErrorMessage = GetError("ER00007");
        RFV_面接日.ErrorMessage = GetError("ER00007");

        string exp = Request.QueryString["mod"];
        if (exp == "exportpdf")
        {
            if (Session["pdfDoc"] != null)
            {
                ExportPDF1((PdfPTable)Session["pdfDoc"]);
            }
        }
        if (Session["役"] == null || Session["役"].ToString() == "")
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        if (!IsPostBack)
        {
            ViewState["listNo"] = new List<string>();
            ddl_組合選択.Focus();
            this.getDDl組合選択();
            hdCount.Value = "0";
            int ddl1 = Convert.ToInt32(ddl_組合選択.SelectedValue);
            int ddl2 = Convert.ToInt32(ddl_組合員企業選択.SelectedValue);
            string strTableHeader = "id2,氏名,ID,生年月日,組合名,企業名,状態,書類承認フラグ";
            ShowGridViewNotClassEmpty(grv, strTableHeader, "データがありません。");
        }

    }

    /// <summary>
    /// GET DATA INTO DDl組合選択
    /// </summary>
    public void getDDl組合選択()
    {
        本社情報Manager frm = new 本社情報Manager();
        DataTable dt = new DataTable();
        dt = frm.frmJ_TraineeAppAssignConfirm_GetDDl組合選択();
        if (dt.Rows.Count > 0)
        {
            ddl_組合選択.DataSource = dt;
            ddl_組合選択.DataTextField = "企業名";
            ddl_組合選択.DataValueField = "企業ID";
            ddl_組合選択.DataBind();

            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_組合選択.Items.Insert(0, 選択);
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    /// <summary>
    /// GET DATA INTO DDl組合員企業選択
    /// </summary>
    public void getDDl組合員企業選択()
    {
        本社情報Manager frm = new 本社情報Manager();
        DataTable dt = new DataTable();
        if (ddl_組合選択.SelectedIndex != 0)
        {
            dt = frm.frmJ_TraineeAppAssignConfirm_GetDDl組合員企業選択(Int32.Parse(ddl_組合選択.SelectedValue));
        }
        else
        {
            dt = frm.frmJ_TraineeAppAssignConfirm_GetDDl組合員企業選択(0);
        }

        //if (dt.Rows.Count > 0)
        {
            ddl_組合員企業選択.DataSource = dt;
            ddl_組合員企業選択.DataTextField = "企業名";
            ddl_組合員企業選択.DataValueField = "企業ID";
            ddl_組合員企業選択.DataBind();

            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_組合員企業選択.Items.Insert(0, 選択);
        }
        if (ddl_組合選択.SelectedValue == "-1")
        {
            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_組合員企業選択.Items.Clear();
            ddl_組合員企業選択.Items.Insert(0, 選択);
            ddl_textbox2.Items.Clear();
            ddl_textbox2.Items.Insert(0, 選択);
            HiddenField2.Value = "";
        }

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    public void getDDl面接日()
    {
        面接情報Manager frm = new 面接情報Manager();
        DataTable dt = new DataTable();
        if (ddl_組合員企業選択.SelectedValue != "-1")
        {
            dt = frm.frmJ_InterviewResultSearch_面接日(Int32.Parse(ddl_組合選択.SelectedValue), Int32.Parse(ddl_組合員企業選択.SelectedValue));
        }
        else
        {
            dt = frm.frmJ_InterviewResultSearch_面接日(0, 0);
        }

        //if (dt.Rows.Count > 0)
        {
            ddl_textbox2.DataSource = dt;
            ddl_textbox2.DataTextField = "面接日";
            ddl_textbox2.DataValueField = "面接日";
            ddl_textbox2.DataBind();

            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_textbox2.Items.Insert(0, 選択);
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    /// <summary>
    /// BIND DATA INTO GRIDVIEW
    /// </summary>

    public void ShowGrid(int int_団体ID, int int_企業ID, string dt_面接日)
    {

        DataTable dt = new DataTable();
        応募情報Manager _応募情報Manager = new 応募情報Manager();
        dt = _応募情報Manager.ShowGrid応募情報(int_団体ID, int_企業ID, Convert.ToDateTime(dt_面接日));
        if (dt.Rows.Count == 0)
        {
            string strTableHeader = "氏名,ID,生年月日,組合名,企業名,状態,id2";
            ShowGridViewNotClassEmpty(grv, strTableHeader, "データがありません。");
            hdCount.Value = "0";
        }
        else
        {
            ViewState["dt_temp"] = dt;
            grv.DataSource = dt.DefaultView;
            grv.DataBind();
        }

    }
    public void ShowGridViewNotClass(GridView grv, string strTableHeader, List<string> list)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        foreach (string str in arrColumnNames)
        {
            dt.Columns.Add(str);
        }
        for (int i = 0; i < list.Count; i++)
        {
            DataRow r = dt.NewRow();
            string[] arrFristRowValues = list[i].Split(',');
            for (int j = 0; j < arrColumnNames.Length; j++)
            {
                if (j < arrFristRowValues.Length)
                    r[arrColumnNames[j]] = arrFristRowValues[j];
            }
            dt.Rows.InsertAt(r, i);
        }
        grv.DataSource = dt;
        grv.DataBind();
    }

    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader, string strEmpty)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        foreach (string str in arrColumnNames)
        {
            dt.Columns.Add(str);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count + 1;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);
    }

    /// <summary>
    /// GET ERROR MESSAGE
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
            error = dt.Rows[0][2].ToString();
        return error;

    }
    /// <summary>
    /// UPDATE DATA INTO TABLE 応募情報
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn一括承認_Click(object sender, EventArgs e)
    {
        応募情報Manager frm = new 応募情報Manager();
        //=====================================================================================
        List<string> list = (System.Collections.Generic.List<string>)ViewState["listNo"];
        DataTable dt_temp = (DataTable)ViewState["dt_temp"];
        for (int i = grv.Rows.Count; i < dt_temp.Rows.Count; i++)
        {
            if (dt_temp.Rows[i]["書類承認フラグ"].ToString() == "1")
            {
                list.Add(dt_temp.Rows[i]["id2"].ToString());
            }
        }
        foreach (GridViewRow row in grv.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
            HiddenField hf = (HiddenField)row.FindControl("HiddenField1");
            int index = Check(list, hf.Value);
            if (cb.Checked)
            {
                if (index == -1)
                {
                    list.Add(hf.Value);
                }
            }
            else
            {
                if (index != -1)
                {
                    list.RemoveAt(index);
                }
            }
        }
        for (int i = 0; i < dt_temp.Rows.Count; i++)
        {
            int ID = Convert.ToInt32(dt_temp.Rows[i]["id2"].ToString());
            frm.Update_TraineeAppAssignConfirm(ID);

            for (int k = 0; k < list.Count; k++)
            {
                if (dt_temp.Rows[i]["id2"].ToString() == list[k].ToString())
                {
                    int ID2 = Convert.ToInt32(list[k].ToString());
                    frm.Update_frmJ_TraineeAppAssignConfirm(ID2);

                    list.RemoveAt(k);
                    break;
                }
            }
        }
        ViewState["listNo"] = list;
        int ddl1 = Convert.ToInt32(ddl_組合選択.SelectedValue);
        int ddl2 = Convert.ToInt32(ddl_組合員企業選択.SelectedValue);
        string dt1 = ddl_textbox2.SelectedValue.ToString();
        this.ShowGrid(ddl1, ddl2, dt1);
        //=====================================================================================
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    /// <summary>
    /// BUTTON SEARCH
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn決定_Click(object sender, EventArgs e)
    {
        ViewState.Clear();
        ViewState["listNo"] = new List<string>();
        try
        {
            int ddl1 = Convert.ToInt32(ddl_組合選択.SelectedValue);
            int ddl2 = Convert.ToInt32(ddl_組合員企業選択.SelectedValue);
            string dt = ddl_textbox2.SelectedValue.ToString();
            this.ShowGrid(ddl1, ddl2, dt);
            //this.ResetForm();

            DataTable dt_応募情報 = new DataTable();
            応募情報Manager _応募情報Manager = new 応募情報Manager();
            dt_応募情報 = _応募情報Manager.ShowGrid応募情報(ddl1, ddl2, Convert.ToDateTime(dt));
            if (dt_応募情報.Rows.Count > 0)
            {
                int cb = 0;
                List<string> list = (System.Collections.Generic.List<string>)ViewState["listNo"];
                int num;
                for (int i = 0; i < dt_応募情報.Rows.Count; i++)
                {
                    if (dt_応募情報.Rows[i]["書類承認フラグ"].ToString() == "1")
                    {
                        list.Add(dt_応募情報.Rows[i]["id2"].ToString());
                        cb++;
                    }

                    num = 0;
                    for (int k = 0; k < list.Count; k++)
                    {
                        if (list[k].ToString() == dt_応募情報.Rows[i]["id2"].ToString())
                        {
                            num++;
                        }
                        if (num == 2)
                        {
                            list.RemoveAt(k);
                        }
                    }
                }
                hdCount.Value = cb.ToString();
            }

        }
        catch (Exception ex) { }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    /// <summary>
    /// EXPORT REPORT FOR CHECKBOX
    /// </summary>
    /// <param name="dt"></param>
    public void ExportPDF(DataTable dt)
    {
        //=====================================================================================
        DataTable dt_temp = (DataTable)ViewState["dt_temp"];
        List<string> list = (System.Collections.Generic.List<string>)ViewState["listNo"];

        int num;
        for (int i = 0; i < dt_temp.Rows.Count; i++)
        {
            //if (dt_temp.Rows[i]["書類承認フラグ"].ToString() == "1")
            //{
            //    list.Add(dt_temp.Rows[i]["id2"].ToString());
            //}

            num = 0;
            for (int k = 0; k < list.Count; k++)
            {
                if (list[k].ToString() == dt_temp.Rows[i]["id2"].ToString())
                {
                    num++;
                }
                if (num == 2)
                {
                    list.RemoveAt(k);
                }
            }
        }

        foreach (GridViewRow row in grv.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
            HiddenField hf = (HiddenField)row.FindControl("HiddenField1");
            int index = Check(list, hf.Value);
            if (cb.Checked)
            {
                if (index == -1)
                {
                    list.Add(hf.Value);
                }
            }
            else
            {
                if (index != -1)
                {
                    list.RemoveAt(index);
                }
            }
        }
        int iIndex = 0;
        int iIndex1 = 0;
        Document pdfDoc = new Document(PageSize.A4, -50f, -50f, 100f, 0f);
        BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font f1 = new iTextSharp.text.Font(bf, 23, Font.NORMAL);
        Font f2 = new iTextSharp.text.Font(bf, 10, Font.NORMAL);

        Chunk c = new Chunk("面接情報確認・承認 \n\n", f1);
        PdfWriter.GetInstance(pdfDoc, Page.Response.OutputStream);

        Paragraph p = new Paragraph();
        p.Alignment = Element.ALIGN_CENTER;
        pdfDoc.Open();
        p.Add(c);
        pdfDoc.Add(p);
    A1:
        PdfPTable table = new PdfPTable(5);
        本社情報Manager frm = new 本社情報Manager();
        table.SetWidths(new int[5] { 4, 3, 3, 5, 4 });
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
        int[] widths = new int[grv.Columns.Count];
        for (int i = 6; i > 1; i--)
        {
            widths[i] = Convert.ToInt32(grv.Columns[i].ItemStyle.Width.Value);
            string cellText = Page.Server.HtmlDecode(grv.HeaderRow.Cells[i].Text);
            PdfPCell cell = new PdfPCell(new Phrase(10, cellText, f2));
            cell.BackgroundColor = new BaseColor(169, 169, 169);
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            table.AddCell(cell);
        }
        //Create the PDF Document   
        // add title
        while (iIndex < dt_temp.Rows.Count)
        {
            for (int k = 0; k < list.Count; k++)
            {
                if (dt_temp.Rows[iIndex]["id2"].ToString() == list[k].ToString())
                {
                    DataRow dr;
                    dr = dt_temp.Rows[iIndex];
                    for (int j = 4; j >= 0; j--)
                    {
                        string cellText = Page.Server.HtmlDecode(dr.ItemArray[j].ToString());
                        iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(10, cellText, f2));
                        cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                        cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                        if ((table.Rows.Count + 1) % 2 != 0)
                        {
                            cell.BackgroundColor = new BaseColor(255, 228, 196);//169-169-169
                        }
                        else
                        {
                            cell.BackgroundColor = BaseColor.WHITE;
                        }
                        table.AddCell(cell);
                    }
                    iIndex1++;
                }
            }
            iIndex++;
            if ((iIndex1 != 0 && iIndex1 % 20 == 0) || iIndex == dt_temp.Rows.Count)
            {
                if (iIndex1 > 20)
                {
                    pdfDoc.NewPage();
                }
                pdfDoc.Add(table);
                goto A1;
            }

        }
        //pdfDoc.Add(table); // add the table
        pdfDoc.Close();
        Page.Response.ContentType = "application/pdf";
        string 組合員企業選択 = frm.getNamereport_企業名英語_luclt(ddl_組合員企業選択.SelectedIndex).ToString();
        string 組合選択 = frm.getNamereport_企業名英語_luclt(ddl_組合選択.SelectedIndex).ToString();
        string name = 組合選択 + "" + 組合員企業選択 + "" + DateTime.Now.Date.ToString("yyyy/MM/dd").Replace("/", "");
        Page.Response.AddHeader("content-disposition", "attachment;filename=" + name + ".pdf");
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Page.Response.Write(pdfDoc);
        Page.Response.End();

    }
    /// <summary>
    /// EXPORT REPORT FOR LINKBUTTON
    /// </summary>
    /// <param name="iIndex"></param>
    public void ExportPDF1(PdfPTable table)
    {
        本社情報Manager frm = new 本社情報Manager();
        BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        Font f1 = new iTextSharp.text.Font(bf, 23, Font.NORMAL);

        //Create the PDF Document
        Document pdfDoc = new Document(PageSize.A4, -50f, -50f, 30f, 0f);
        PdfWriter.GetInstance(pdfDoc, Page.Response.OutputStream);
        pdfDoc.Open();
        Chunk c = new Chunk("面接情報確認・承認 \n\n", f1);
        Paragraph p = new Paragraph();
        p.Alignment = Element.ALIGN_CENTER;
        p.Add(c);
        pdfDoc.Add(p); // add title
        pdfDoc.Add(table); // add the table
        pdfDoc.Close();
        Page.Response.ContentType = "application/pdf";
        string 組合員企業選択 = frm.getNamereport_企業名英語_luclt(ddl_組合員企業選択.SelectedIndex).ToString();
        string 組合選択 = frm.getNamereport_企業名英語_luclt(ddl_組合選択.SelectedIndex).ToString();
        string name = 組合選択 + "" + 組合員企業選択 + "" + DateTime.Now.Date.ToString("yyyy/MM/dd").Replace("/", "");
        Page.Response.Write("<script>window.location.href = 'frmJ_TraineeAppAssignConfirm.aspx'</script>");
        Page.Response.AddHeader("content-disposition", "attachment;filename=" + name + ".pdf");
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Page.Response.Write(pdfDoc);
        Page.Response.End();
    }

    /// <summary>
    /// BUTTON EXPORT REPORT
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPDF一括出力_Click(object sender, EventArgs e)
    {
        DataTable dt = ConvertListToDataTable();
        ExportPDF(dt);
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    /// <summary>
    /// GET DATA FROM GRIDVIEW TO DATATABLE
    /// </summary>
    /// <returns></returns>
    public DataTable ConvertListToDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("氏名", typeof(string));
        dt.Columns.Add("ID", typeof(string));
        dt.Columns.Add("生年月日", typeof(string));
        dt.Columns.Add("組合名", typeof(string));
        dt.Columns.Add("企業名", typeof(string));

        List<List<String>> list = new List<List<string>>();
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            dt.Rows.Add(grv.Rows[i].Cells[2].Text, grv.Rows[i].Cells[3].Text, grv.Rows[i].Cells[4].Text, grv.Rows[i].Cells[5].Text, grv.Rows[i].Cells[6].Text);
        }
        return dt;
    }
    /// <summary>
    /// PAGING GRIDVIEW
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)// phan trang.
    {
        //grv.PageIndex = e.NewPageIndex;
        //DataTable dt = (DataTable)ViewState["CurrentTable"];
        //DataView dv = new DataView(dt);
        //dv.Sort = "ID DESC";
        //grv.DataSource = dv;
        //grv.DataSource = dt;
        //grv.DataBind();
        List<string> listNo = (System.Collections.Generic.List<string>)ViewState["listNo"];
        foreach (GridViewRow row in grv.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
            HiddenField hf = (HiddenField)row.FindControl("HiddenField1");
            int index = Check(listNo, hf.Value);
            if (cb.Checked)
            {
                if (index == -1)
                {
                    listNo.Add(hf.Value);
                }
            }
            else
            {
                if (index != -1)
                {
                    listNo.RemoveAt(index);
                }
            }
        }
        ViewState["listNo"] = listNo;

        string pageId = string.Format("Page{0}", grv.PageIndex);
        bool[] selectedCheckboxes = new bool[grv.PageSize];
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            TableCell cell = grv.Rows[i].Cells[0];
            selectedCheckboxes[i] = (cell.FindControl("CheckBox1") as CheckBox).Checked;
        }

        ViewState[pageId] = selectedCheckboxes;
        grv.PageIndex = e.NewPageIndex;

        int ddl1 = Convert.ToInt32(ddl_組合選択.SelectedValue);
        int ddl2 = Convert.ToInt32(ddl_組合員企業選択.SelectedValue);
        string dt = ddl_textbox2.SelectedValue.ToString();
        this.ShowGrid(ddl1, ddl2, dt);

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }
    public int Check(List<string> list, string label)
    {
        int temp = -1;
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == label)
            {
                temp = i;
                break;
            }
        }
        return temp;
    }
    /// <summary>
    /// RESET FORM
    /// </summary>
    public void ResetForm()
    {
        ddl_組合選択.Text = "-1";
        ddl_組合員企業選択.Text = "-1";
        ddl_textbox2.Text = "-1";
    }
    /// <summary>
    /// EVENT CLICK FOR LINKBUTTON
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lk1_Click(object sender, CommandEventArgs e)
    {
        int iIndex = Convert.ToInt32(e.CommandArgument);

        DataTable dt = new DataTable();
        dt.Columns.Add("氏名", typeof(string));
        dt.Columns.Add("ID", typeof(string));
        dt.Columns.Add("生年月日", typeof(string));
        dt.Columns.Add("組合名", typeof(string));
        dt.Columns.Add("企業名", typeof(string));

        dt.Rows.Add(grv.Rows[iIndex].Cells[2].Text, grv.Rows[iIndex].Cells[3].Text, grv.Rows[iIndex].Cells[4].Text, grv.Rows[iIndex].Cells[5].Text, grv.Rows[iIndex].Cells[6].Text);
        BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        Font f2 = new iTextSharp.text.Font(bf, 10, Font.NORMAL);
        PdfPTable table = new PdfPTable(5);
        table.SetWidths(new int[5] { 4, 3, 3, 5, 4 });
        table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
        int[] widths = new int[grv.Columns.Count];
        for (int i = 6; i > 1; i--)
        {
            widths[i] = Convert.ToInt32(grv.Columns[i].ItemStyle.Width.Value);
            string cellText = Page.Server.HtmlDecode(grv.HeaderRow.Cells[i].Text);
            PdfPCell cell = new PdfPCell(new Phrase(10, cellText, f2));
            cell.BackgroundColor = new BaseColor(169, 169, 169);
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            table.AddCell(cell);
        }
        //Transfer rows from GridView to table
        if (grv.Rows[iIndex].RowType == DataControlRowType.DataRow)
        {
            DataRow dr;
            dr = dt.Rows[0];
            for (int j = 4; j >= 0; j--)
            {
                string cellText = Page.Server.HtmlDecode(dr.ItemArray[j].ToString());
                iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(10, cellText, f2));
                cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.BackgroundColor = BaseColor.WHITE;
                table.AddCell(cell);
            }
        }
        Session.Add("pdfDoc", table);
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "window.open('frmJ_TraineeAppAssignConfirm.aspx?mod=exportpdf')", true);
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }

    protected void grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        CheckBox cb = (CheckBox)e.Row.FindControl("CheckBox1");
        if (cb != null)
        {
            DataRowView dtv = (DataRowView)e.Row.DataItem;
            cb.Checked = dtv.Row.ItemArray[5].ToString() == "0" ? false : true;
            if (e.Row.RowIndex != 0)
                ScriptManager.RegisterStartupScript(this, this.GetType(), "starScript", "CheckIsCheckBox();", true);
        }
    }
    protected void ddl_組合選択_SelectedIndexChanged(object sender, EventArgs e)
    {
        getDDl組合員企業選択();
        if (ddl_組合選択.SelectedIndex == 0)
        {
            ddl_組合員企業選択.Items.Clear();
            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_組合員企業選択.Items.Insert(0, 選択);
            ddl_組合選択.Focus();
        }
        else
        {
            ddl_組合員企業選択.Focus();
        }
        string strTableHeader = "id2,氏名,ID,生年月日,組合名,企業名,状態,書類承認フラグ";
        ShowGridViewNotClassEmpty(grv, strTableHeader, "データがありません。");
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
        RFV_ddl_組合選択.Validate();


    }
    protected void grv_PreRender(object sender, EventArgs e)
    {
        string pageId = string.Format("Page{0}", grv.PageIndex);
        bool[] selectedCheckboxes = ViewState[pageId] as bool[];
        if (selectedCheckboxes != null)
        {
            for (int i = 0; i < grv.Rows.Count; i++)
            {
                TableCell cell = grv.Rows[i].Cells[0];
                (cell.FindControl("CheckBox1") as CheckBox).Checked = selectedCheckboxes[i];
            }
        }
    }

    protected void ddl_組合員企業選択_SelectedIndexChanged(object sender, EventArgs e)
    {
        getDDl面接日();
        HiddenField2.Value = "";
        if (ddl_組合員企業選択.SelectedIndex == 0)
        {
            ddl_textbox2.Items.Clear();
            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_textbox2.Items.Insert(0, 選択);
            ddl_組合員企業選択.Focus();

        }
        else
        {
            面接情報Manager frm = new 面接情報Manager();
            DataTable dt = frm.frmJ_InterviewResultSearch_面接日(Convert.ToInt32(ddl_組合選択.SelectedValue), Convert.ToInt32(ddl_組合員企業選択.SelectedValue));
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    HiddenField2.Value += row["面接日"].ToString() + ",";
                }
            }
            ddl_textbox2.Focus();
        }
        string strTableHeader = "id2,氏名,ID,生年月日,組合名,企業名,状態,書類承認フラグ";
        ShowGridViewNotClassEmpty(grv, strTableHeader, "データがありません。");
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
        RFV_組合員企業選択.Validate();

    }
    protected void ddl_textbox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_textbox2.SelectedValue.ToString() != "-1")
        {
            ShowGrid(Int32.Parse(ddl_組合選択.SelectedValue), Int32.Parse(ddl_組合員企業選択.SelectedValue), ddl_textbox2.SelectedValue);

            DataTable dt_応募情報 = new DataTable();
            応募情報Manager _応募情報Manager = new 応募情報Manager();
            dt_応募情報 = _応募情報Manager.ShowGrid応募情報(Int32.Parse(ddl_組合選択.SelectedValue), Int32.Parse(ddl_組合員企業選択.SelectedValue), Convert.ToDateTime(ddl_textbox2.SelectedValue));
            if (dt_応募情報.Rows.Count > 0)
            {
                int cb = 0;
                List<string> list = (System.Collections.Generic.List<string>)ViewState["listNo"];
                int num;
                for (int i = 0; i < dt_応募情報.Rows.Count; i++)
                {
                    if (dt_応募情報.Rows[i]["書類承認フラグ"].ToString() == "1")
                    {
                        list.Add(dt_応募情報.Rows[i]["id2"].ToString());
                        cb++;
                    }

                    num = 0;
                    for (int k = 0; k < list.Count; k++)
                    {
                        if (list[k].ToString() == dt_応募情報.Rows[i]["id2"].ToString())
                        {
                            num++;
                        }
                        if (num == 2)
                        {
                            list.RemoveAt(k);
                        }
                    }
                }
                hdCount.Value = cb.ToString();
            }
        }
        else
        {
            string strTableHeader = "id2,氏名,ID,生年月日,組合名,企業名,状態,書類承認フラグ";
            ShowGridViewNotClassEmpty(grv, strTableHeader, "データがありません。");
        }
        ddl_textbox2.Focus();
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "registerStartup();", true);
    }


}

